package AWT.计算器;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class jsq {
    public static void main(String[] args) {
        new ck();
    }
}

class ck extends Frame {
    wbk wbk = new wbk();

    public ck() {
        super("计算器");
        setBounds(100, 100, 500, 300);
        setLayout(new GridLayout(2, 1));
        add(wbk);
        add(new an());
        setVisible(true);

    }

    public class an extends Panel {
        public an() {
            super(new GridLayout(4, 4));

            Button button0 = new Button("0");
            Button button1 = new Button("1");
            Button button2 = new Button("2");
            Button button3 = new Button("3");
            Button button4 = new Button("4");
            Button button5 = new Button("5");
            Button button6 = new Button("6");
            Button button7 = new Button("7");
            Button button8 = new Button("8");
            Button button9 = new Button("9");
            Button ja = new Button("+");
            Button jan = new Button("-");
            Button cen = new Button("*");
            Button cu = new Button("/");
            Button dian = new Button(".");
            Button dy = new Button("=");


            add(button1);
            add(button2);
            add(button3);
            add(ja);

            add(button4);
            add(button5);
            add(button6);
            add(jan);

            add(button7);
            add(button8);
            add(button9);
            add(cen);

            add(dian);
            add(button0);
            add(cu);
            add(dy);

            Mymonitor mymonitor = new Mymonitor();
            button0.addActionListener(mymonitor);
            button1.addActionListener(mymonitor);
            button2.addActionListener(mymonitor);
            button3.addActionListener(mymonitor);
            button4.addActionListener(mymonitor);
            button5.addActionListener(mymonitor);
            button6.addActionListener(mymonitor);
            button7.addActionListener(mymonitor);
            button8.addActionListener(mymonitor);
            button9.addActionListener(mymonitor);
            ja.addActionListener(mymonitor);
            jan.addActionListener(mymonitor);
            cen.addActionListener(mymonitor);
            cu.addActionListener(mymonitor);
            dian.addActionListener(mymonitor);
            dy.addActionListener(mymonitor);

            addWindowListener(new cksj1());
        }

        public class cksj1 implements WindowListener {

            @Override
            public void windowOpened(WindowEvent e) {

            }

            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println(e);
                System.exit(1);
            }

            @Override
            public void windowClosed(WindowEvent e) {

            }

            @Override
            public void windowIconified(WindowEvent e) {

            }

            @Override
            public void windowDeiconified(WindowEvent e) {

            }

            @Override
            public void windowActivated(WindowEvent e) {

            }

            @Override
            public void windowDeactivated(WindowEvent e) {

            }
        }
    }

    public class wbk extends TextField {
        public TextField textField;

        public void sr(String wb) {
            textField.setText("4444");
            textField.setText(textField.getText() + wb);
        }
    }

    public class Mymonitor implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
//        e.getActionCommand()获得信息（默认按钮和文本框的内容名字）
//        System.out.println(e.getActionCommand());
            System.out.println("按钮被点击");
            if (e.getActionCommand().equals("=")) {
                String nr1 = wbk.getText();
                System.out.println(nr1);
                String nr = String.valueOf(new sshu().sshu(nr1));
                wbk.setText(nr);
            } else {
                wbk.setText(wbk.getText() + e.getActionCommand());
            }

//        System.out.println(fa.getText());//获取输入框的文本
//        fa.setText(null);//清除输入的内容，设置文本框内容为null
        }
    }

    public class sshu {
        public int sshu(String ss) {
//            StringTokenizer str=new StringTokenizer(ss, "+");
//            while(str.hasMoreTokens()){
//                System.out.println(str.nextToken());
//            }
            int sum = 0;
            String[] str = ss.split("[*/+-]");
            String[] str1 = ss.split("[^-+*/]");
            for (String s : str) {
                System.out.println(s + "...........");
            }

            for (String s : str1) {
                System.out.println(s + ".......");

            }
            sum = (int) Double.parseDouble(str[0]);
//            System.out.println(str[0]+"=str");
//            System.out.println(str1[0]);
//            System.out.println(str1[1]);
            for (int i = 0,j=1; i < str1.length; i++) {
                    if (str1[i].equals("+")) {
                        sum = (int) (Double.parseDouble(str[j]) + sum);
                        j++;
                        System.out.println("+");
                    } else if (str1[i].equals("-")) {
                        sum = (int) (Double.parseDouble(str[j]) - sum);
                        j++;
                        System.out.println("-");
                    } else if (str1[i].equals("*")) {
                        sum = (int) (Double.parseDouble(str[j]) * sum);
                        j++;
                        System.out.println("*");
                    } else if (str1[i].equals("/")) {
                        sum = (int) (Double.parseDouble(str[j]) % sum);
                        j++;
                        System.out.println("/");
                    } else {
                        System.out.println("没有符号");
                    }
            }
            return sum;
        }
    }

}


